<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户订单流水号" align="center" prop="mchOrderNo" />
      <el-table-column label="订单时间" align="center" prop="mchReqTime" />
      <el-table-column label="请求扣款金额" align="center" prop="orderAmount" />
      <el-table-column label="实际扣款金额" align="center" prop="orderAmountReal" />
      <el-table-column label="商户订单描述" align="center" prop="orderDesc" />
      <el-table-column label="签约银行卡编号" align="center" prop="signNo" />
      <el-table-column label="扣款发起状态" align="center" prop="requestStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.request_status" :value="scope.row.requestStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="扣款状态" align="center" prop="deductionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.deduction_status" :value="scope.row.deductionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="扣款失败说明" align="center" prop="failDesc"/>
      <el-table-column label="发起时间" align="center" prop="requestTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/deduction/orderdetail";

export default {
  name: "Record",
  dicts: ['deduction_status','request_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扣款请求明细日志表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deductionOrderDetailRecordCode: null,
        deductionOrderRecordId: null,
        deductionOrderId: null,
        mchOrderNo: null,
        mchReqTime: null,
        orderAmount: null,
        orderAmountReal: null,
        orderDesc: null,
        signNo: null,
        requestStatus: null,
        deductionStatus: null,
        failDesc: null,
        requestTime: null,
        requestData: null,
        callbackTime: null,
        callbackData: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductionOrderDetailRecordCode: [
          { required: true, message: "扣款请求明细记录code不能为空", trigger: "blur" }
        ],
        deductionOrderRecordId: [
          { required: true, message: "扣款订单记录id不能为空", trigger: "blur" }
        ],
        deductionOrderId: [
          { required: true, message: "扣款订单id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询扣款请求明细日志列表 */
    getList() {
      this.loading = true;
      const deductionOrderRecordId = this.$route.params && this.$route.params.deductionOrderRecordId;
      if(deductionOrderRecordId){
        this.queryParams.deductionOrderRecordId = deductionOrderRecordId;
      }
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deductionOrderDetailRecordId: null,
        deductionOrderDetailRecordCode: null,
        deductionOrderRecordId: null,
        deductionOrderId: null,
        mchOrderNo: null,
        mchReqTime: null,
        orderAmount: null,
        orderAmountReal: null,
        orderDesc: null,
        signNo: null,
        requestStatus: "0",
        deductionStatus: "0",
        failDesc: null,
        requestTime: null,
        requestData: null,
        callbackTime: null,
        callbackData: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deductionOrderDetailRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扣款请求明细日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deductionOrderDetailRecordId = row.deductionOrderDetailRecordId || this.ids
      getRecord(deductionOrderDetailRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扣款请求明细日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deductionOrderDetailRecordId != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deductionOrderDetailRecordIds = row.deductionOrderDetailRecordId || this.ids;
      this.$modal.confirm('是否确认删除扣款请求明细日志编号为"' + deductionOrderDetailRecordIds + '"的数据项？').then(function() {
        return delRecord(deductionOrderDetailRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有扣款请求明细日志数据项？').then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
